package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.InsuranceApprovalConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.InsuranceApprovalDTO;
import com.mobile.service.dto.InsuranceApprovalQueryDTO;
import com.mobile.service.entity.InsuranceApproval;
import com.mobile.service.inter.InsuranceApprovalService;
import com.mobile.service.mapper.InsuranceApprovalMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 保险审核表service实现类
 */
@Service("insuranceApprovalService")
@Transactional
public class InsuranceApprovalServiceImpl implements InsuranceApprovalService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private InsuranceApprovalConverter insuranceApprovalConverter;
    @Autowired
    private InsuranceApprovalMapper insuranceApprovalMapper;

    @Override
    public Map<String, Object> addInsuranceApproval(InsuranceApprovalDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        InsuranceApprovalDTO pd = insuranceApprovalMapper.getInsuranceApprovalByAuditorName(dto.getAuditorName());
        if (null == pd) {
            insuranceApprovalMapper.insertInsuranceApproval(insuranceApprovalConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该审核人姓名"));
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<InsuranceApprovalDTO> getInsuranceApprovalListPage(InsuranceApprovalQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<InsuranceApprovalDTO> list = insuranceApprovalMapper.insuranceApprovallistPage(queryDTO);
        return new PageResult<InsuranceApprovalDTO>(list);
    }

    @Override
    public InsuranceApprovalDTO getInsuranceApprovalById(Integer id) throws Exception {
        return insuranceApprovalMapper.getInsuranceApprovalById(id);
    }

    @Override
    public Map<String, Object> updateInsuranceApproval(InsuranceApprovalDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Integer pkValue = dto.getId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        InsuranceApprovalDTO data = this.getInsuranceApprovalById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        InsuranceApprovalDTO pd = insuranceApprovalMapper.getInsuranceApprovalByAuditorName(dto.getAuditorName());
        if (null == pd || pd.getId().equals(pkValue)) {
            insuranceApprovalMapper.updateInsuranceApproval(insuranceApprovalConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该审核人姓名"));
        }
        return map;
    }

    @Override
    public Map<String, Object> removeInsuranceApproval(Integer id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        insuranceApprovalMapper.deleteInsuranceApproval(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<InsuranceApprovalDTO> getInsuranceApprovalList(InsuranceApprovalQueryDTO queryDTO) throws Exception {
        return insuranceApprovalMapper.getInsuranceApprovalList(queryDTO);
    }

    @Override
    public void insertInsuranceApproval(InsuranceApproval insuranceApproval) throws Exception {
        insuranceApprovalMapper.insertInsuranceApproval(insuranceApproval);
    }
}
